import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// import legacy from '@vitejs/plugin-legacy'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    // legacy({
    //   targets: ['Chrome >= 59']
    // }),
    vue(),
    (function () {
      let basePath = "";
      return {
        name: "ceshi2:micro-app",
        apply: "build",
        configResolved(config) {
          basePath = `${config.base}${config.build.assetsDir}/`;
        },
        renderChunk(code, chunk) {
          if (
            chunk.fileName.endsWith(".js") &&
            /(from|import)(\s*['"])(\.\.?\/)/g.test(code)
          ) {
            code = code.replace(
              /(from|import)(\s*['"])(\.\.?\/)/g,
              (all, $1, $2, $3) => {
                return all.replace($3, new URL($3, basePath));
              }
            );
          }
          return code;
        },
      };
    })(),
  ],
  server: {
    host: "0.0.0.0",
    port: 7002,
  },
  build: {
    outDir: "../dist/ceshi2",
  },
  clearScreen: false,
  base: `${
    process.env.NODE_ENV === "production"
      ? "http://192.168.60.31:5500/dist"
      : ""
  }/ceshi2/`,
});
